// $Id: Unit1.pas 62 2006-07-27 16:54:30Z alpov $

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    btnOpen: TButton;
    txbData1: TEdit;
    btnSend: TButton;
    txbItemLo: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    btnClose: TButton;
    chkData1: TRadioButton;
    chkData2: TRadioButton;
    txbData2: TEdit;
    Timer1: TTimer;
    txbResponse: TMemo;
    Label4: TLabel;
    rgpType: TRadioGroup;
    rgpCommand: TRadioGroup;
    IdLabel: TLabel;
    lbxItemHi: TListBox;
    chkTXT: TCheckBox;
    chkData0: TRadioButton;
    rgpDest: TRadioGroup;
    procedure btnOpenClick(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure rgpCommandClick(Sender: TObject);
  private
    FSequence: Byte;
  end;

const
  ITEMy: array[0..4, 0..5] of Byte = (
    (10, 20, 30, 40, 255, 0),
    (10, 20, 30, 40, 255, 0),
    (10, 20, 30, 0, 0, 0),
    (10, 11, 20, 21, 0, 0),
    (10, 11, 20, 21, 0, 0)
  );
  COMMANDy: array[0..4] of Byte = (120, 121, 110, 100, 101);

var
  Form1: TForm1;

implementation

uses Serial, proto;

{$R *.dfm}

procedure TForm1.btnOpenClick(Sender: TObject);
begin
  if not OpenCom('COM1', 19200) then ShowMessage('nelze otevrit')
                                else btnOpen.Enabled := False;
  btnClose.Enabled := not btnOpen.Enabled;
end;

procedure TForm1.btnSendClick(Sender: TObject);
var
  ph: PACKET_HEADER;
  data: array[0..255] of Byte;
  c: Cardinal;
  len: Byte;
begin
  FillChar(data, sizeof(data), 0);
  len := 0;

  ph.seq := FSequence;
  case rgpDest.ItemIndex of
    0: ph.dest := ADDR_TERMINAL;
    1: ph.dest := ADDR_SERVER;
  end;
  ph.src := ADDR_PC;
  ph.cmd := COMMANDy[rgpCommand.ItemIndex];
  ph.item_hi := ITEMy[rgpCommand.ItemIndex, lbxItemHi.ItemIndex];
  ph.item_lo := StrToInt(txbItemLo.Text);
  if chkData0.Checked then begin
    len := 0;
  end else if chkData1.Checked then begin
    FillChar(data, sizeof(data), 0);
    StrPCopy(@data[0], txbData1.Text);
    len := 16; //Length(txbData1.Text);
  end else begin
    c := StrToInt(txbData2.Text);
    case rgpType.ItemIndex of
      0: begin
           data[0] := Byte((c and $000000ff));
           len := 1;
         end;
      1: begin
           data[1] := Byte((c and $0000ff00) shr 8);
           data[0] := Byte((c and $000000ff));
           len := 2;
         end;
      2: begin
           data[3] := Byte((c and $ff000000) shr 24);
           data[2] := Byte((c and $00ff0000) shr 16);
           data[1] := Byte((c and $0000ff00) shr 8);
           data[0] := Byte((c and $000000ff));
           len := 4;
         end;
    end;
  end;

  SerialSendPacket(ph, @data, len);
  Inc(FSequence);
end;

procedure TForm1.btnCloseClick(Sender: TObject);
begin
  CloseCom;
  btnOpen.Enabled := True;
  btnClose.Enabled := not btnOpen.Enabled;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  S, S2: string;
  Len, I: Integer;
  B: array[0..1023] of Byte;
  ph: PACKET_HEADER;
begin
  S := '';
  S2 := '';
  FillChar(B, sizeof(B), 0);
  Len := WaitingOnCom;
  if Len > 0 then begin
    ReadCom(B, Len);
    for I := 0 to Len - 1 do begin
      if (B[I] = $AA) and (B[I+1] = $AA) and (S2 <> '') then begin
        if chkTXT.Checked then
          S := S + ' [' + S2 + ']' + #13#10
        else
          S := S + #13#10;
        S2 := '';
      end;
      S := S + IntToHex(B[I], 2) + ' ';
      if (B[I] in [32..127]) then
        S2 := S2 + Chr(B[I])
      else
        S2 := S2 + '.';
    end;
{
    if (B[0] = $AA) and
       (B[1] = $AA) and
       (B[4] = ADDR_PC) and
       (B[6] in [120])
    then begin
      ph.seq := B[3];
      ph.src := B[5];
      SerialSendAck(ph);
      S := 'ACK SEQ ' + IntToStr(ph.seq) + ' --- ' + S;
    end;
}
  end;
  if S <> '' then
    if chkTXT.Checked then
      txbResponse.Lines.Add(S + ' [' + S2 + ']')
    else
      txbResponse.Lines.Add(S);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FSequence := 0;
  rgpCommandClick(Sender);
end;

procedure TForm1.rgpCommandClick(Sender: TObject);
begin
  with lbxItemHi.Items do begin
    Clear;
    case rgpCommand.ItemIndex of
      0, 1: begin
        Add('ITEM_MODE');
        Add('ITEM_SHIFT');
        Add('ITEM_SWITCH');
        Add('ITEM_RTC');
        Add('ITEM_ALL');
      end;
      2: begin
        Add('ITEM_TEMPERATURE');
        Add('ITEM_MESSAGE');
        Add('ITEM_STATE');
      end;
      3, 4: begin
        Add('ITEM_SENSOR_COUNT');
        Add('ITEM_SENSOR_DESC');
        Add('ITEM_MODE_COUNT');
        Add('ITEM_MODE_DESC');
      end;
    end;
  end;
end;

end.
